﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MPMovieCatalog.Service;
using MPMovieCatalog.Domain;
using MPMovieCatalog.Util;

namespace MPMovieCatalog.Forms
{
    public partial class FileChooseForm : Form
    {
        public FileChooseForm()
        {
            InitializeComponent();
            UpdateUI();
        }

        public ArrayList Files
        {
            get
            {
                ArrayList files = new ArrayList();
                foreach (ListViewItem item in filesListView.SelectedItems)
                {
                    if (item.Tag != null && !(item.Tag is Share))
                    {
                        ShareFile shareFile = (ShareFile)item.Tag;
                        files.Add(shareFile);
                    }
                }
                return files;
            }
        }

        private void FileChooseForm_Load(object sender, EventArgs e)
        {
            TreeNode root = new TreeNode("root", 2, 2);
            sharedFoldersTreeView.Nodes.Add(root);

            ArrayList shares = MovieServiceFactory.getMovieService().GetShares();
            foreach (Share share in shares)
            {
                TreeNode node = root.Nodes.Add(string.Format("{0} ({1})", share.Name, share.Path));
                node.Tag = share;
                WalkShareTree(share, node);
            }
        }

        private void WalkShareTree(Share rootShare, TreeNode rootNode)
        {
            foreach (Share childShare in rootShare.Children)
            {
                TreeNode childNode = rootNode.Nodes.Add(childShare.Name);
                childNode.Tag = childShare;
                WalkShareTree(childShare, childNode);
            }
        }

        private void sharedFoldersTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            filesListView.Items.Clear();
            if (sharedFoldersTreeView.SelectedNode != null && sharedFoldersTreeView.SelectedNode.Text != string.Empty)
            {
                foreach (TreeNode node in sharedFoldersTreeView.SelectedNode.Nodes)
                {
                    Share childShare = (Share)node.Tag;
                    if (childShare != null)
                    {
                        ListViewItem item = new ListViewItem(childShare.Name, 0);
                        item.Tag = childShare;
                        filesListView.Items.Add(item);
                    }
                }

                Share share = (Share)sharedFoldersTreeView.SelectedNode.Tag;
                if (share != null)
                {
                    ArrayList shareFiles = MovieServiceFactory.getMovieService().GetFiles(share.Path, "*.*");
                    foreach (ShareFile shareFile in shareFiles)
                    {
                        ListViewItem item = new ListViewItem(shareFile.Name, 1);
                        item.Tag = shareFile;
                        filesListView.Items.Add(item);
                    }
                }
            }

        }

        private void filesListView_DoubleClick(object sender, EventArgs e)
        {
            if (filesListView.SelectedItems != null && filesListView.SelectedItems.Count > 0)
            {
                if (filesListView.SelectedItems[0].Tag is Share)
                {
                    Share share = (Share)filesListView.SelectedItems[0].Tag;
                    sharedFoldersTreeView.SelectedNode.Expand();
                    foreach (TreeNode node in sharedFoldersTreeView.SelectedNode.Nodes)
                    {
                        Share childShare = (Share)node.Tag;
                        if (childShare != null && share.Name.Equals(childShare.Name))
                        {
                            sharedFoldersTreeView.SelectedNode = node;
                            break;
                        }
                    }
                }
            }
        }

        private void filesListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            fileTextBox.Clear();
            StringBuilder sb = new StringBuilder();
            foreach (ListViewItem item in filesListView.SelectedItems)
            {
                if (item.Tag != null && !(item.Tag is Share))
                {
                    ShareFile shareFile = (ShareFile)item.Tag;
                    sb.Append("\"");
                    sb.Append(shareFile.Name);
                    sb.Append("\" ");
                }

            }
            fileTextBox.Text = sb.ToString().Trim();
        }

        private void buttonPanel_Paint(object sender, PaintEventArgs e)
        {
            ControlPaint.DrawBorder3D(e.Graphics, this.buttonPanel.ClientRectangle, Border3DStyle.Etched, Border3DSide.Top);
        }

        private void buttonPanel_Resize(object sender, EventArgs e)
        {
            Invalidate();
        }

        private void UpdateUI()
        {
            this.Text = ResourceUtils.GetString("chooseFile", 0);
            fileLabel.Text = ResourceUtils.GetString("chooseFile", 1);
            okButton.Text = ResourceUtils.GetString("chooseFile", 2);
            cancelButton.Text = ResourceUtils.GetString("chooseFile", 3);
        }
    }
}
